from django.shortcuts import render,redirect
from app01 import models
from app01.utils.pagination import Pagination
from app01.utils.form import UserModelForm,PrettyModelForm,PrettyEditModelForm

def depart_list(request):
    #  去数据库获取所有部门信息
    queryset = models.Department.objects.all()
    return render(request,'depart_list.html',{'queryset':queryset})
def depart_add(request):
    if request.method == "GET":
        return render(request,"depart_add.html")
    title = request.POST.get("title")
    models.Department.objects.create(title=title)
    return redirect("/depart/list/")
def depart_delete(request):
    nid = request.GET.get("nid")
    models.Department.objects.filter(id=nid).delete()
    return redirect("/depart/list/")
def depart_edit(request,nid):
    if request.method == "GET":
        row_object = models.Department.objects.filter(id=nid).first()
        return render(request,"depart_edit.html",{"row_object":row_object})
    title = request.POST.get("title")
    models.Department.objects.filter(id=nid).update(title = title)
    return redirect("/depart/list/")

def user_list(request):
    #  去数据库获取所有部门信息
    queryset = models.UserInfo.objects.all()

    page_object = Pagination(request,queryset,page_size=5)
    context = {
        'queryset':page_object.page_queryset,
        "page_string":page_object.html()
    }

    # for obj in queryset:
    #     print(obj.create_time.strftime("%Y-%m-%d"))
    #     print(obj.get_gender_display())
    #     print(obj.depart.title)
    return render(request,'user_list.html',context)
def user_add(request):   
    if request.method == "GET":
        context = {
            'genter_choices':models.UserInfo.gender_choices,
            'depart_list':models.Department.objects.all()
        }
        return render(request,"user_add.html",context)
    user = request.POST.get("user")
    pwd = request.POST.get("pwd")
    age = request.POST.get("age")
    account = request.POST.get("ac")
    ctime = request.POST.get("ctime")
    gender_id = request.POST.get("gd")
    depart_id = request.POST.get("dp")
    models.UserInfo.objects.create(name=user,password=pwd,age=age,account=account,
                                   create_time=ctime,gender=gender_id,depart_id=depart_id)
    return redirect("/user/list/")
def user_model_form_add(request):
    if request.method == "GET":
        form = UserModelForm()
        return render(request,"user_model_form_add.html",{'form' : form})
    form = UserModelForm(data = request.POST)
    if form.is_valid():
        # print(form.cleaned_data)
        form.save()
        return redirect("/user/list/")
    else:
        return render(request,"user_model_form_add.html",{'form' : form})
def user_edit(request,nid):
    row_object = models.UserInfo.objects.filter(id=nid).first()
    if request.method == "GET":        
        form = UserModelForm(instance = row_object)
        return render(request,"user_edit.html",{'form' : form})
    form = UserModelForm(data = request.POST,instance = row_object)
    if form.is_valid():
        # print(form.cleaned_data)
        form.save()
        return redirect("/user/list/")
    else:
        return render(request,"user_edit.html",{'form' : form})   
def user_delete(request,nid):
    models.UserInfo.objects.filter(id=nid).delete()
    return redirect("/user/list/")

def pretty_list(request):

    # for i in range(300):
    #     models.PrettyNum.objects.create(mobile="18702994877",price=10,level=1,status=1)

    data_dict = {}
    search_data = request.GET.get("q","")
    if search_data:
        data_dict = {"mobile__contains": search_data}
    queryset = models.PrettyNum.objects.filter(**data_dict).order_by("-level")
        
    page_object = Pagination(request,queryset)
    context = {
        "search_data":search_data,
        'queryset':page_object.page_queryset,
        "page_string":page_object.html()
    }    
    
    return render(request,'pretty_list.html',context)
def pretty_add(request):
    if request.method == "GET":
        form = PrettyModelForm()
        return render(request,"pretty_add.html",{'form' : form})
    form = PrettyModelForm(data = request.POST)
    if form.is_valid():
        # print(form.cleaned_data)
        form.save()
        return redirect("/pretty/list/")
    else:
        return render(request,"pretty_add.html",{'form' : form})
def pretty_edit(request,nid):
    row_object = models.PrettyNum.objects.filter(id=nid).first()
    if request.method == "GET":        
        form = PrettyEditModelForm(instance = row_object)
        return render(request,"pretty_edit.html",{'form' : form})
    form = PrettyEditModelForm(data = request.POST,instance = row_object)
    if form.is_valid():
        # print(form.cleaned_data)
        form.save()
        return redirect("/pretty/list/")
    else:
        return render(request,"pretty_edit.html",{'form' : form})   
def pretty_delete(request,nid):
    models.PrettyNum.objects.filter(id=nid).delete()
    return redirect("/pretty/list/")

